package com.yunfei.schedule.crmToApp;
import com.yunfei.bo.CrmAcceDtlBo;
import com.yunfei.bo.TblobstorageBo;
import com.yunfei.bo.TblobstorageSimpleBo;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.aiiidb.CrmAcceDtlMapper;
import com.yunfei.mapper.crmdb.TblobtSorageMapper;
import com.yunfei.utils.ComponentFactory;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.DownloadPdf;
import com.yunfei.utils.string.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.List;

@Component
public class ScheduledTaskToday {
    @Value("${file.ip.address}")
    String ipAddress;
    @Value("${file.savePath}")
    String savePath;
    @Value("${file.prexDomain}")
    String prexDomain;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTaskToday.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

 /*  @Scheduled(cron = "0 0 1 * * ?")  */ //每天凌晨1点执行
   @Scheduled(cron = "0 0/1 * * * ?")   //每1分钟
  /* @Scheduled(fixedRate = 60000)*/
    public void testMyBatis() {
        System.out.println("ScheduledTaskToday定时从crm文件服务器获取当天的所有附件信息"+ DateUtil.getDateNow("yyyy-MM-dd HH:mm:ss"));
        int newCount=0;
        int oldCount=0;
        try {
            CrmAcceDtlMapper crmAcceDtlMapper = ComponentFactory.getBean(CrmAcceDtlMapper.class);
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            TblobstorageBo queryParam=new TblobstorageBo();
            List<TblobstorageSimpleBo> list=tblobtSorageMapper.findTodayCrmFiles(queryParam);//crm所有附件信息
            //客服app所有附件信息
            int size=0;
            if(!StringUtil.isNullOrEmpty(list)){
                size=list.size();
            }
            if(size>0){
                System.out.println("总条数========================"+size);
                for(int i=0;i<size;i++){
                    TblobstorageSimpleBo tblobstorageBo=list.get(i);
                    CrmAcceDtlBo crmFileBo=new CrmAcceDtlBo();
                    String lStorageid=tblobstorageBo.getlStorageid();
                    crmFileBo.setLStorageid(lStorageid);
                    crmFileBo.setFileUrl(prexDomain);
                    String cFilePath=tblobstorageBo.getcFilepath();
                    String appPath=tblobstorageBo.getcAppPath();
                    //获取原始文件存放地址
                    String oFileUrl=ipAddress+"/"+cFilePath.replaceAll("\\\\","/")+"baccessory";
                    String fileName=tblobstorageBo.getcFilename();
                    String localFilePath=savePath+appPath;
                    localFilePath=localFilePath.replaceAll("\\\\","/")+"/";
                    boolean flag=DownloadPdf.downLoadServerFileToLocal(oFileUrl,fileName,localFilePath);
                    //if(flag){
                        //文件在crm服务器存在
                        //判断文件是否再app已经存在
                        CrmAcceDtlBo query=new CrmAcceDtlBo();
                        query.setLStorageid(lStorageid);
                        int count=crmAcceDtlMapper.count(query);
                        if(count==0){
                            crmAcceDtlMapper.add(crmFileBo);
                            newCount++;
                            //更新crm附件状态为已同步c_objtype
                            TblobstorageEntity tblobstorageEntity=new TblobstorageEntity();
                            tblobstorageEntity.setlStorageid(lStorageid);
                            tblobstorageEntity.setcObjtype("Y");
                            tblobtSorageMapper.update(tblobstorageEntity);
                        }else{
                            oldCount++;
                        }
                    //}else{
                       // CrmAcceDtlBo updateParam=new CrmAcceDtlBo();
                     //   updateParam.setLStorageid(lStorageid);
                     //   updateParam.setFileUrl(prexDomain+appPath.replaceAll("\\\\","/")+"/"+fileName);
                     //   crmAcceDtlMapper.update(updateParam);

                  //  }


                }
                try{
                    //程序暂时半秒再执行
                    Thread.sleep(500);//半秒
                    crmAcceDtlMapper.updateSubkeyId();
                }catch(Exception e){
                    //System.exit(0);//退出程序
                }

            }

            if (list != null) {
                System.out.println(list.size()+"条任务处理完成,其中新增 "+newCount+"条,已存在 "+oldCount+"条");
            } else {
                System.out.println("我什么事都不用做，等待下次再说吧");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

